/*
   Copyright (c) 2008-1015 Alex Zhondin <qtinuum.team@gmail.com>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

#ifndef QI_RADIO_H
#define QI_RADIO_H

#include "core/ext/ViewModeled.h"
#include "core/ext/ControllerMousePushable.h"
#include "core/ext/ModelCallback.h"
#include "core/ext/ModelStore.h"

namespace Qi
{

class QI_EXPORT ModelRadio: public ModelComparable
{
    Q_OBJECT
    Q_DISABLE_COPY(ModelRadio)

public:
    bool isRadioItem(ID id) const { return isRadioItemImpl(id); }
    bool setRadioItem(ID id) { return setRadioItemImpl(id); }

protected:
    ModelRadio() {}

    int compareImpl(ID left, ID right) const override;
    bool isAscendingDefaultImpl(ID /*id*/) const override { return false; }

    virtual bool isRadioItemImpl(ID id) const = 0;
    virtual bool setRadioItemImpl(ID id) = 0;
};

class QI_EXPORT ModelRadioCallback: public ModelRadio
{
    Q_OBJECT
    Q_DISABLE_COPY(ModelRadioCallback)

public:
    ModelRadioCallback() {}

    std::function<bool(ID)> isRadioItem;
    std::function<bool(ID)> setRadioItem;

protected:
    bool isRadioItemImpl(ID id) const override;
    bool setRadioItemImpl(ID id) override;
};

class QI_EXPORT ModelRadioStorage: public ModelRadio
{
    Q_OBJECT
    Q_DISABLE_COPY(ModelRadioStorage)

public:
    ModelRadioStorage(ID radioId);

    ID radioItem() const { return m_radioId; }

protected:
    bool isRadioItemImpl(ID id) const override;
    bool setRadioItemImpl(ID id) override;

private:
    ID m_radioId;
};

class QI_EXPORT ViewRadio: public ViewModeled<ModelRadio>
{
    Q_OBJECT
    Q_DISABLE_COPY(ViewRadio)

public:
    ViewRadio(const SharedPtr<ModelRadio>& model, bool useDefaultController = true);

protected:
    QSize sizeImpl(const GuiContext& ctx, ID id, ViewSizeMode sizeMode) const override;
    void drawImpl(QPainter* painter, const GuiContext& ctx, const CacheContext& cache, bool* showTooltip) const override;

private:
    QStyle::State styleState(ID id) const;

    PushableTracker m_pushableTracker;
};

QI_EXPORT SharedPtr<ControllerMousePushable> createControllerMouseRadio(SharedPtr<ModelRadio> model);

} // end namespace Qi

#endif // QI_VIEW_RADIO_H
